import pandas as pd
import matplotlib.pyplot as plt
# Load your trade data
file_path_user = r"../data/01_01_2007-24_05_2024.csv"
try:
    trade_data_user = pd.read_csv(file_path_user, encoding='utf-8', error_bad_lines=False, warn_bad_lines=True)
except UnicodeDecodeError:
    trade_data_user = pd.read_csv(file_path_user, encoding='latin1', error_bad_lines=False, warn_bad_lines=True)

# Ensure the closing_time_utc is properly converted to datetime format
trade_data_user['closing_time_utc'] = pd.to_datetime(trade_data_user['closing_time_utc'])

# Sort the dataframe by closing time to ensure the plot is in the correct order
trade_data_user_sorted = trade_data_user.sort_values(by='closing_time_utc')

# Calculate cumulative profit
trade_data_user_sorted['cumulative_profit'] = trade_data_user_sorted['profit_usd'].cumsum()

# Plotting the cumulative profit over time
plt.figure(figsize=(10, 6))
plt.plot(trade_data_user_sorted['closing_time_utc'], trade_data_user_sorted['cumulative_profit'], marker='o', linestyle='-', markersize=2)
plt.title('EXNESS Cumulative Profit Over Time')
plt.xlabel('Closing Time (UTC)')
plt.ylabel('Cumulative Profit (USD)')
plt.xticks(rotation=45)
plt.grid(True)


# Calculate the total time period of the data
start_date = trade_data_user['closing_time_utc'].min()
end_date = trade_data_user['closing_time_utc'].max()

# Format the dates as yy:mm:dd
start_date_formatted = start_date.strftime('%y-%m-%d')
end_date_formatted = end_date.strftime('%y-%m-%d')

# Calculate the total time period in days
total_period_days = (end_date - start_date).days

print(f"Start Date: {start_date_formatted}")
print(f"End Date: {end_date_formatted}")
print(f"Total Time Period: {total_period_days} days")
textstr = f'Start Date: {start_date_formatted}\nEnd Date: {end_date_formatted}\nTotal Period: {total_period_days} days'
plt.gcf().text(0.15, 0.85, textstr, fontsize=12, bbox=dict(facecolor='gray', alpha=0.5),position = (0.2,0.6))
plt.show()